package com.lanyou.esb.entity;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.hibernate.annotations.GenericGenerator;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.lanyou.cook.common.DefaultDateJsonSerializer;

/**
 * 接口请求量
 * 
 * @author 杨晨睿
 */

@Entity
@EntityListeners({ AuditingEntityListener.class })
@Table(name = "T_ESB_BU_IF_REQUEST_QTY")
public class IfReqQuantity {

	private String id;
	private String requestThreshold;
	private String requestMax;
	private String requestUnit;
	private Http http;
	private String isEnable;
	private String creator;
	private Date createTime;
	private String modifier;
	private Date modifyTime;

	
	public final static Specification<IfReqQuantity> EmptySpecification = new Specification<IfReqQuantity>(){
		@Override
		public Predicate toPredicate(Root<IfReqQuantity> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
			return null;
		}
	};
	
	public static Specification<IfReqQuantity> idSpecification(String id){
		return new Specification<IfReqQuantity>(){
			@Override
			public Predicate toPredicate(Root<IfReqQuantity> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				query.where(cb.equal(root.get("id"), id));
				return null;
			}
		};
	}
	
	
	@Id
	@GeneratedValue(generator = "uuidGenerator")
	@GenericGenerator(name = "uuidGenerator", strategy = "uuid")
	@Column(name = "IF_REQUEST_QTY_ID", length = 36)
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}


	@Column(name = "REQUEST_THRESHOLD", length = 11)
	public String getRequestThreshold() {
		return requestThreshold;
	}

	public void setRequestThreshold(String requestThreshold) {
		this.requestThreshold = requestThreshold;
	}

	@Column(name = "MAX_REQUEST_QTY", length = 11)
	public String getRequestMax() {
		return requestMax;
	}

	public void setRequestMax(String requestMax) {
		this.requestMax = requestMax;
	}

	@Column(name = "REQUEST_UNIT", length = 10)
	public String getRequestUnit() {
		return requestUnit;
	}

	public void setRequestUnit(String requestUnit) {
		this.requestUnit = requestUnit;
	}

	@ManyToOne
	@JoinColumn(name = "IF_ID")
	public Http getHttp() {
		return http;
	}

	public void setHttp(Http http) {
		this.http = http;
	}

	@Column(name = "IS_ENABLE", length = 2)
	public String getIsEnable() {
		return isEnable;
	}

	public void setIsEnable(String isEnable) {
		this.isEnable = isEnable;
	}

	@CreatedBy
	@Column(name = "CREATOR", length = 50, updatable=false)
	public String getCreator() {
		return creator;
	}

	public void setCreator(String creator) {
		this.creator = creator;
	}

	@LastModifiedBy
	@Column(name = "MODIFIER", length = 50)
	public String getModifier() {
		return modifier;
	}

	public void setModifier(String modifier) {
		this.modifier = modifier;
	}

	@Column(name = "CREATED_DATE", updatable=false)
	@JsonSerialize(using = DefaultDateJsonSerializer.class)
	@CreatedDate
	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	@Column(name = "LAST_UPDATED_DATE")
	@JsonSerialize(using = DefaultDateJsonSerializer.class)
	@LastModifiedDate
	public Date getModifyTime() {
		return modifyTime;
	}

	public void setModifyTime(Date modifyTime) {
		this.modifyTime = modifyTime;
	}

	@Transient
	public String getCreateTimeForExport(){
		if(createTime != null){
			return createTime.toString().substring(0, createTime.toString().length() - 2);
		}else{
			return "";
		}
		
	}
	
	@Transient
	public String getModifyTimeForExport(){
		if(modifyTime != null){
			return modifyTime.toString().substring(0, modifyTime.toString().length() - 2);
		}else{
			return "";
		}
	}
}